home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / TinyGL.lha / tinygl / src / nglx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-07  |  3.0 KB  |  129 lines

  1. /* simple glx like driver for TinyGL and Nano X */
  2. #include <GL/gl.h>
  3. #include <GL/nglx.h>
  4. #include <microwin/nano-X.h>
  5. #include "zgl.h"
  6.  
  7. typedef struct {
  8.     GLContext *gl_context;
  9.     int xsize,ysize;
  10.     GR_DRAW_ID drawable;
  11.     GR_GC_ID gc;
  12.     int pixtype; /* pixel type in TinyGL */
  13. } TinyNGLXContext;
  14.  
  15. NGLXContext nglXCreateContext(NGLXContext shareList, int flags)
  16. {
  17.   TinyNGLXContext *ctx;
  18.  
  19.   if (shareList != NULL) {
  20.     gl_fatal_error("No sharing available in TinyGL");
  21.   }
  22.   ctx=gl_malloc(sizeof(TinyNGLXContext));
  23.   if (!ctx)
  24.       return NULL;
  25.   ctx->gl_context=NULL;
  26.   return (NGLXContext) ctx;
  27. }
  28.  
  29. void glXDestroyContext( NGLXContext ctx1 )
  30. {
  31.   TinyNGLXContext *ctx = (TinyNGLXContext *) ctx1;
  32.   if (ctx->gl_context != NULL) {
  33.     glClose();
  34.   }
  35.   gl_free(ctx);
  36. }
  37.  
  38.  
  39. /* resize the glx viewport : we try to use the xsize and ysize
  40.    given. We return the effective size which is guaranted to be smaller */
  41.  
  42. static int glX_resize_viewport(GLContext *c,int *xsize_ptr,int *ysize_ptr)
  43. {
  44.   TinyNGLXContext *ctx;
  45.   int xsize,ysize;
  46.   
  47.   ctx=(TinyNGLXContext *)c->opaque;
  48.  
  49.   xsize=*xsize_ptr;
  50.   ysize=*ysize_ptr;
  51.  
  52.   /* we ensure that xsize and ysize are multiples of 2 for the zbuffer. 
  53.      TODO: find a better solution */
  54.   xsize&=~3;
  55.   ysize&=~3;
  56.  
  57.   if (xsize == 0 || ysize == 0) return -1;
  58.  
  59.   *xsize_ptr=xsize;
  60.   *ysize_ptr=ysize;
  61.  
  62.   ctx->xsize=xsize;
  63.   ctx->ysize=ysize;
  64.  
  65.   /* resize the Z buffer */
  66.   ZB_resize(c->zb,NULL,xsize,ysize);
  67.   return 0;
  68. }
  69.  
  70. /* we assume here that drawable is a window */
  71. int nglXMakeCurrent( NGLXDrawable drawable,
  72.                      NGLXContext ctx1)
  73. {
  74.   TinyNGLXContext *ctx = (TinyNGLXContext *) ctx1;
  75.   int mode, xsize, ysize;
  76.   ZBuffer *zb;
  77.   GR_WINDOW_INFO win_info;
  78.  
  79.   if (ctx->gl_context == NULL) {
  80.       /* create the TinyGL context */
  81.       GrGetWindowInfo(drawable, &win_info);
  82.  
  83.       xsize = win_info.width;
  84.       ysize = win_info.height;
  85.  
  86.       /* currently, we only support 16 bit rendering */
  87.       mode = ZB_MODE_5R6G5B;
  88.       zb=ZB_open(xsize,ysize,mode,0,NULL,NULL,NULL);
  89.       if (zb == NULL) {
  90.           fprintf(stderr, "Error while initializing Z buffer\n");
  91.           exit(1);
  92.       }
  93.  
  94.       ctx->pixtype = MWPF_TRUECOLOR565;
  95.  
  96.       /* create a gc */
  97.       ctx->gc = GrNewGC();
  98.       
  99.       /* initialisation of the TinyGL interpreter */
  100.       glInit(zb);
  101.       ctx->gl_context=gl_get_context();
  102.       ctx->gl_context->opaque=(void *) ctx;
  103.       ctx->gl_context->gl_resize_viewport=glX_resize_viewport;
  104.  
  105.       /* set the viewport : we force a call to glX_resize_viewport */
  106.       ctx->gl_context->viewport.xsize=-1;
  107.       ctx->gl_context->viewport.ysize=-1;
  108.       
  109.       glViewport(0, 0, xsize, ysize);
  110.   }
  111.   
  112.   return 1;
  113. }
  114.  
  115. void nglXSwapBuffers( NGLXDrawable drawable )
  116. {
  117.     GLContext *gl_context;
  118.     TinyNGLXContext *ctx;
  119.     
  120.     /* retrieve the current NGLXContext */
  121.     gl_context=gl_get_context();
  122.     ctx=(TinyNGLXContext *)gl_context->opaque;
  123.     
  124.     GrArea(drawable, ctx->gc, 0, 0, ctx->xsize, 
  125.            ctx->ysize, ctx->gl_context->zb->pbuf, ctx->pixtype);
  126. }
  127.  
  128.  
  129.